<template>
  <!-- 网站首页工作动态、学术活动、研究生重要信息、本科生重要信息板块 -->
  <div class="main">
    <div class="top">
      <div class="column-name">
        <img :src="imageurl" alt="" class="column-image"/>
        <h2>{{ message }}</h2>
      </div>

      <div class="more-link">
        <router-link :to="`${path}`">
          <span>更多&gt;&gt;</span>
        </router-link>
      </div>
    </div>

    <!-- 文章列表 -->
    <div class="content">
      <ul v-if="articles.length > 0" class="article-list">
        <li
          v-for="article in articles"
          :key="article.article_id"
          class="article-item"
        >
          <el-tooltip placement="bottom" effect="light" :content="article.title" :visible-arrow="false"
                      :open-delay="1000">
            <router-link
              :to="`${path}/details/${article.article_id}`"
              class="article-link"
            >
              {{ truncatedTitle(article.title) }}

            </router-link>
          </el-tooltip>

          <span class="publish-date">{{
              formatDate(article.publish_date)
            }}</span>


        </li>
      </ul>
      <div v-else class="no-articles">暂无文章。</div>
    </div>
  </div>
</template>

<script>
import service from "@/utils/service";

export default {
  props: ["message", "imageurl", "path","subcolumn_id"],//从父组件接收的属性
  data() {
    return {
      articles: [],
    };
  },
  //只显示前五条数据，是一个计算属性
  computed: {
    limitedArticles() {
      return this.articles.slice(0, 8);
    },
  },

  methods: {
    //请求文章列表方法
    getArticlesBySubcolumn(subcolumn_id) {
      return service
        .get("/articles/list", {
          params: {
            subcolumn_id: subcolumn_id,
          },
        })
        .then((response) => {
          if (response.data.code === 20041) {
            this.articles = response.data.data;
            console.log("主页信息板块的文章",this.articles)
          } else {
            console.error("出错了 Response code:", response.status);
          }
        })
        .catch((error) => {
          console.error("出错了", error.message);
        });
    },
    //格式化日期
    formatDate(dateString) {
      const date = new Date(dateString);
      const year = date.getFullYear();
      const month = (date.getMonth() + 1).toString().padStart(2, "0");
      const day = date.getDate().toString().padStart(2, "0");
      return `${year}-${month}-${day}`;
    },
    //截断标题
    truncatedTitle(title) {
      const maxLength = 28;
      if (title.length > maxLength) {
        return title.slice(0, maxLength) + "...";
      }
      return title;
    },

  },

  mounted() {
    this.getArticlesBySubcolumn(this.subcolumn_id);
  },
};
</script>

<style scoped>
.main {
  width: 720px;
  height: 350px;
  border: none;
  border-radius: 10px;
}

.top {
  display: flex;
  width: 100%;
  align-items: center;
  padding-left: 0;
  justify-content: space-between;
}

.column-name {
  display: flex;
  align-items: center;
  line-height: 20px;
  font-size: 14px;
  padding: 10px;
  padding-bottom: 8px;
  border-bottom: 3px solid #1a64b3;
}

ul {
  padding-inline-start: 30px;
  line-height: 20px;
  list-style: none;
  list-style-image: url(../assets//空五角星.png);
}

.more-link {
  display: flex;
  align-items: center;
  padding-right: 15px;
}

.more-link span:hover{
  color: #FFB300;
}

.column-image {
  width: 40px;
  height: 40px;
  object-fit: cover;
  margin-right: 10px;
}

.publish-date {
  float: right;
  padding-right: 5px;
}

a {
  text-decoration: none;
  color: #000;
}

.article-item {
  padding: 5px;
  font-size: 15px;
  line-height: 1.5;
}

.el-divider {
  margin: 0px;
}

.content {
  padding-top: 8px;
}

.article-item:hover .article-link {
  color: rgb(241, 173, 89);
}

h2{
  color:#0668CC;
}


</style>
